package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;
import com.vilyever.drawingview.brush.drawing.PenBrush;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.ColorAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.databinding.ActivityPicGraffitiBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class PicGraffitiActivity extends BaseAc<ActivityPicGraffitiBinding> {

    public static String imgPath = "";
    private PenBrush penBrush;
    private int paintWidth = 15;//画笔宽度
    private int eraserWidth = 15;//橡皮擦宽度
    private ColorAdapter colorAdapter;
    private int oldPosition = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_graffiti;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicGraffitiBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivPicGraffitiSave.setOnClickListener(this);
        mDataBinding.ivGraffitiBrush.setOnClickListener(this);
        mDataBinding.ivGraffitiEraser.setOnClickListener(this);
        mDataBinding.rvColorList.setLayoutManager(new GridLayoutManager(mContext, 7));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        colorAdapter.getItem(oldPosition).setSelect(false);
        colorAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        colorAdapter.getItem(position).setSelect(true);
        colorAdapter.notifyItemChanged(position);
        penBrush.setColor(Color.parseColor(colorAdapter.getItem(position).getColor()));//默认画笔颜色
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivPicGraffitiSave://保存
                saveImg();
                break;
            case R.id.ivGraffitiBrush://画笔
                clearView();
                mDataBinding.ivGraffitiBrush.setImageResource(R.drawable.aicon_huabi2);
                mDataBinding.sbBrush.setVisibility(View.VISIBLE);
                penBrush.setIsEraser(false);
                penBrush.setSize(paintWidth);
                break;
            case R.id.ivGraffitiEraser://橡皮擦
                clearView();
                mDataBinding.ivGraffitiEraser.setImageResource(R.drawable.aicon_xiangpi2);
                mDataBinding.sbEraser.setVisibility(View.VISIBLE);
                penBrush.setIsEraser(true);
                penBrush.setSize(eraserWidth);
                break;
        }
    }

    @Override
    protected void initData() {
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        Glide.with(this).load(imgPath).into(mDataBinding.imageViewTouch);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        initPen();
        //颜色数据
        List<MyColorBean> listColor = new ArrayList<>();
        listColor.add(new MyColorBean("#FFFFFF", true));
        listColor.add(new MyColorBean("#000000", false));
        listColor.add(new MyColorBean("#FF7A7A", false));
        listColor.add(new MyColorBean("#FFCA7B", false));
        listColor.add(new MyColorBean("#F7FF84", false));
        listColor.add(new MyColorBean("#95D764", false));
        listColor.add(new MyColorBean("#FFA8A8", false));
        listColor.add(new MyColorBean("#51FFB0", false));
        listColor.add(new MyColorBean("#6DF9FF", false));
        listColor.add(new MyColorBean("#446AFF", false));
        listColor.add(new MyColorBean("#A362FF", false));
        listColor.add(new MyColorBean("#E661FF", false));
        listColor.add(new MyColorBean("#00A27F", false));
        listColor.add(new MyColorBean("#FF5555", false));
        colorAdapter.setList(listColor);
    }

    //初始化画笔
    private void initPen() {
        penBrush = PenBrush.defaultBrush();
        penBrush.setColor(Color.parseColor("#FFFFFF"));//默认画笔颜色
        penBrush.setSize(15);//默认画笔大小
        mDataBinding.myDrawingView.setBrush(penBrush);
        //设置画笔大小
        mDataBinding.sbBrush.setMax(50);
        mDataBinding.sbBrush.setProgress(5);
        mDataBinding.sbBrush.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                paintWidth = 5 + i * 2;
                penBrush.setSize(paintWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //设置橡皮擦大小
        mDataBinding.sbEraser.setMax(50);
        mDataBinding.sbEraser.setProgress(5);
        mDataBinding.sbEraser.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                eraserWidth = 5 + i * 2;
                penBrush.setSize(eraserWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_picture_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlGraffitiSaveView);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //管理控件
    private void clearView() {
        mDataBinding.ivGraffitiBrush.setImageResource(R.drawable.aicon_huabi1);
        mDataBinding.ivGraffitiEraser.setImageResource(R.drawable.aicon_xiangpi1);
        mDataBinding.sbBrush.setVisibility(View.GONE);
        mDataBinding.sbEraser.setVisibility(View.GONE);
    }

}